<template>
<!-- 发布需求时--最终用到的表单 -->
  <div class="input-form whole-form">
    <el-form :model="ruleForm" style="width: 65%;" :rules="rules" ref="ruleForm" label-width="100px"
             class="demo-ruleForm">
      <el-form-item label="需求标题" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>

      <el-form-item label="预算（...元）" prop="budget">
        <el-input v-model="ruleForm.budget"></el-input>
      </el-form-item>

      <el-form-item label="期望完成时间" required>
        <el-col :span="11">
          <el-form-item prop="date1">
            <el-date-picker type="date" placeholder="截止日期" v-model="ruleForm.date1"
                            style="width: 100%;"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col class="line" :span="2">————</el-col>
        <el-col :span="11">
          <el-form-item prop="date2">
            <el-time-picker placeholder="截止时间" v-model="ruleForm.date2" style="width: 100%;"></el-time-picker>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="需求描述" prop="desc">
        <el-input type="textarea" v-model="ruleForm.desc"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即发布</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "form2",
  data() {
    return {
      // 表单数据
      ruleForm: {
        name: '',
        budget: '',
        date1: '',
        date2: '',
        desc: ''
      },
      // 表单数据的限制
      rules: {
        name: [
          {required: true, message: '请输入需求标题', trigger: 'blur'},
          {min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur'}
        ],
        budget: [
          {type: 'string', required: true, message: '请输入大致预算金额', trigger: 'change'}
        ],
        date1: [
          {type: 'date', required: true, message: '请选择日期', trigger: 'change'}
        ],
        date2: [
          {type: 'date', required: true, message: '请选择时间', trigger: 'change'}
        ],
        desc: [
          {required: true, message: '请填写需求描述', trigger: 'blur'}
        ]
      },
      //返回的结果
      resp:[],
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
            if (valid) {
              // this.orderAdd();
              // this.$message('添加成功');
            } else {
              // 添加失败的情况
              console.log('error submit!!');
              return false;
            }
          }
      )
      ;
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //发布/修改订单的方法 2个方法很相似，就是接口/参数  及链接
    orderAdd(){
      // url不确定，需要加上
      axios.post('', this.ruleForm).then(res => {
        if (res.status == 200) {
          this.resp = res.data;
          this.$alert('添加成功', 'OK', {
            confirmButtonText: '确定',
            callback: action => {
              this.$router.push('/home')
            }
          });
        }
      }).catch(err => {
        console.log(err.message)
      })
    }
  }
}
</script>

<style scoped>
.whole-form {
  /*background-image: url("@/assets/02.webp");*/
}
.input-form {
  /*增加阴影*/
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.5);
  /*增加圆弧角  500px 750px*/
  border-radius: 5px;
  height: 68.5%;
  width: 60%;
  max-width: 100%;
  padding: 0px 35px 0;
  margin: auto;
  position: absolute;
  top: 13%;
  left: 15%;
}

</style>